package com.example.myapplication

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.myapplication.fragments.YYYYYFragment
import com.example.myapplication.fragments.ZZZZZZFragment

class MainActivity : AppCompatActivity() {
    companion object {
        const val TAG = "MainActivity"
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        Log.d(TAG, intent.toString())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate: ")
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        findViewById<Button>(R.id.go_A).setOnClickListener {
            startActivity(Intent(this, MainActivity::class.java))
        }
        findViewById<Button>(R.id.go_B).setOnClickListener {
            startActivity(Intent(this, BBBB::class.java))
        }
        findViewById<Button>(R.id.frag_Z_add).setOnClickListener {
            supportFragmentManager.fragments.add(0,ZZZZZZFragment())
            supportFragmentManager.beginTransaction().commit()
        }
        findViewById<Button>(R.id.frag_Y_add).setOnClickListener {
            supportFragmentManager.beginTransaction()
                .replace(R.id.fragmentContainerView_4, YYYYYFragment(), "Y")
                .commit()
        }
        findViewById<Button>(R.id.frag_Y_remove).setOnClickListener {
            val fragment = supportFragmentManager.findFragmentByTag("Y")
            supportFragmentManager.popBackStack()
            Log.d(TAG, "frag_Y_remove Tag y fragment is ${fragment.toString()} ")
            fragment?.let{
                supportFragmentManager.beginTransaction().show(fragment).commit()
                supportFragmentManager.beginTransaction().remove(it).commit()
            }
        }
        findViewById<Button>(R.id.btn1).setOnClickListener {

        }
        findViewById<Button>(R.id.btn2).setOnClickListener {

        }
        findViewById<Button>(R.id.btn3).setOnClickListener {
        }
    }

    override fun onRestart() {
        super.onRestart()
        Log.d(TAG, "onRestart: ")
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart: ")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume: ")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop: ")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause: ")
    }

    override fun finish() {
        super.finish()
        Log.d(TAG, "finish: ")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy: ")
    }
}